#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
import pycurl

class getCurl:
    def __init__(self,url):
        self.url = url

    def main(self):
        c = pycurl.Curl()
        c.setopt(pycurl.URL,self.url)
        c.setopt(pycurl.CONNECTTIMEOUT,5)
        c.setopt(pycurl.TIMEOUT,5) #定义请求超时时间
        c.setopt(pycurl.NOPROGRESS,1)
        c.setopt(pycurl.FORBID_REUSE,1) #定义交互后强制断开连接
        c.setopt(pycurl.MAXREDIRS,1) #定义最大重定向次数
        c.setopt(pycurl.ENCODING,'') #定义支持的编码格式
        c.setopt(pycurl.HTTPHEADER,['Accept:','Accept-Language:zh-CN,zh;q=0.8']) #定义请求头
        indexfile = open(os.path.dirname(__file__)+'/'+'index.html','wb')
        c.setopt(pycurl.WRITEHEADER,indexfile)
        c.setopt(pycurl.WRITEDATA,indexfile)
        try:
            c.perform()
        except Exception as e:
            print(str(e))
            indexfile.close()
            c.close()
            sys.exit(1)
        lookup_time = c.getinfo(c.TOTAL_TIME)
        http_code = c.getinfo(c.HTTP_CODE)
        size_download = c.getinfo(c.SIZE_DOWNLOAD)
        header_size = c.getinfo(c.HEADER_SIZE)
        speed_download = c.getinfo(c.SPEED_DOWNLOAD)
        print("http code: %s" % http_code)
        print("lookup time: %s" % lookup_time)
        print("size download: %s" % size_download)
        print("header size: %s" % header_size)
        print("speed download: %s" % speed_download)
        indexfile.close()
        c.close()
if __name__ == '__main__':
    url = ""
    curl = getCurl(url)
    curl.main()